/**
 * 
 */
package leetCode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhong
 *
 */
public class LinkedListComponents {
	public class ListNode {
		int val;
		ListNode next;

		ListNode(int x) {
			val = x;
		}
	}

	public int numComponents(ListNode head, int[] G) {
		Set<Integer> gSet = new HashSet<>();
		for (int integer : G) {
			gSet.add(integer);
		}
		int res = 0;
		ListNode cur = head;
		while (cur != null) {
			// 跳过不在g的
			while (cur != null && !gSet.contains(cur.val)) {
				cur = cur.next;
			}
			// 连续在g的
			if (cur != null) {
				res++;
				while (cur != null && gSet.contains(cur.val)) {
					cur = cur.next;
				}
			}
		}
		return res;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
